Ext.define('cloud.controller.Login', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.data.JsonP'],
    config: {
        refs: {
            mainview: 'mainview',
            userview: 'userview',
            txt_name: '#txt_name',
            txt_password: '#txt_password',
            btn_register: '#btn_register',
            btn_login: '#btn_login'
        },
        control: {
            btn_login: {
                tap: 'btn_login_ontap'
            }
        },
        routes: {
            'login': 'login'
        }
    },

    btn_login_ontap: function () {
        var nameValue = this.getTxt_name().getValue(),
            passwordValue = this.getTxt_password().getValue();
        var user = Ext.create('cloud.model.Login', {name: nameValue, password: passwordValue});
        var errors = user.validate();
        if (errors.isValid()) {
            Ext.data.JsonP.request({
                url: GLOBAL.api_url + 'manager_login',
                method: 'post',
                params: {
                    //传递参数
                    username: nameValue,
                    password: passwordValue
                },
                scope: this,
                success: function (data) {
                    if (data.Result.code == 0) {
                        //储存当前登录用户信息
                        localStorage.setItem('username', nameValue);
                        localStorage.setItem('userinfo', data.Variables);
                        Ext.Viewport.animateActiveItem({xtype: 'main'}, {type: 'slide', direction: 'left'});
                    }
                    else
                        Ext.Msg.alert(data.Message.messagestr);
                },
                failure: function () {
                    //请求失败时执行操作
                    Ext.Msg.alert('请求失败', '服务器维护中....');
                }
            });

        }
        else {
            var message = "";
            Ext.each(errors.items, function (rec) {
                message += rec.getMessage() + "<br>";
            });
            Ext.Msg.alert("验证失败", message);
        }
    }
});